// window.onload=function load() {
//     check(clickEle);
// };


var xhr = null;
// 判断手机号是否注册过
function checkTelephone(){
    var registerPhone = document.getElementById("registerPhone").value;
    var phoneRegex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    document.getElementById("registerPassword").placeholder='请输入您的手机号'

    if(registerPhone.length ==0 ){
        document.getElementById("error-tip1").innerText = "手机号不能为空";
        document.getElementById("sendButton").disabled=true;
        document.getElementById("sendButton").style.color="white";
        document.getElementById("registerButton").disabled=true;
        document.getElementById("registerButton").style.cursor="auto";
    }else{
        if(!(phoneRegex.test(registerPhone))){
            document.getElementById("error-tip1").innerText = "手机号格式错误";
            document.getElementById("sendButton").style.color="white";
            document.getElementById("sendButton").disabled=true;
            document.getElementById("sendButton").style.cursor="auto";
            document.getElementById("registerButton").disabled=true;
            document.getElementById("registerButton").style.cursor="auto";
        }else{

            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else{
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }
            var url = "/client/isClientRegister?registerPhone="+registerPhone;
            xhr.open("GET",url,true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
            xhr.onreadystatechange = processTelephone;
            xhr.send();
        }
    }
}

function processTelephone() {

    if(xhr.readyState == 4 && xhr.status == 200) {
        var result = JSON.parse(xhr.responseText);

        if(result == 0){  //客户还未注册时,表示客户可以注册
            document.getElementById("error-tip1").innerText = null;
            document.getElementById("sendButton").disabled=false;//设置发送验证码按钮可用
            document.getElementById("sendButton").style.cursor="pointer";//设置验证码按钮小手状态
            document.getElementById("registerButton").disabled=false;//设置提交按钮可用
            document.getElementById("registerButton").style.cursor="pointer";//设置提交按钮小手状态
        }else{            //客户已经注册,表示客户不可以注册
            document.getElementById("registerButton").style.color="black";
            document.getElementById("error-tip1").innerText = "手机号已经被使用,请重新输入";
            document.getElementById("sendButton").style.cursor="auto";  //设置按钮箭头状态
            document.getElementById("sendButton").disabled=true;//设置按钮不可用
            document.getElementById("registerButton").style.cursor="auto";  //设置按钮箭头状态
            document.getElementById("registerButton").disabled=true;//设置按钮不可用
        }
    }
}

// 判断密码
function checkPassword() {
    var registerPassword = document.getElementById("registerPassword").value;
    var passwordRegex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
    document.getElementById("registerPassword").placeholder='请设置登录密码'
    if(registerPassword.length==0){
        document.getElementById("error-tip2").innerText="密码不能为空";
        document.getElementById("registerButton").disabled=true;
        document.getElementById("registerButton").style.cursor="auto";
    }else if(!(passwordRegex.test(registerPassword))){
        document.getElementById("error-tip2").innerText="密码至少为8位且必须含有字母,数字";
        //让注册按钮不可用
        document.getElementById("registerButton").disabled=true;
        document.getElementById("registerButton").style.cursor="wait";
    }else{
        document.getElementById("error-tip2").innerText=null;
        document.getElementById("sendButton").style.cursor="pointer";
        document.getElementById("registerButton").disabled=false;
        document.getElementById("registerButton").style.cursor="pointer";
    }
}

var InterValObj; //timer变量，控制时间
var count=59;
var curCount;

//检查获得验证码按钮()
function getVerificationCode() {
    var registerPhone = document.getElementById("registerPhone").value;
    var phoneRegex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;

    if(registerPhone.length==0){
        document.getElementById("sendButton").style.color="white";
        document.getElementById("sendButton").disabled=true;
        document.getElementById("sendButton").style.cursor="auto";
        document.getElementById("error-tip1").innerText="手机号不能为空";
    }else{
        if(!(phoneRegex.test(registerPhone))) {
            document.getElementById("sendButton").style.color="white";
            document.getElementById("sendButton").disabled=true;
            document.getElementById("sendButton").style.cursor="auto";
            document.getElementById("error-tip1").innerText = "手机号格式错误";
        }else{
            document.getElementById("error-tip1").innerText = null;

            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else{
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }

            curCount=count;
            document.getElementById("sendButton").style.color="white";
            document.getElementById("sendButton").disabled = true;
            document.getElementById("sendButton").style.cursor="wait";
            document.getElementById("sendButton").value=curCount+"s重新发送";
            InterValObj=window.setInterval(SetRemainTime,1000);
            var url = "/client/checkVerificationCode?registerPhone="+registerPhone;
            xhr.open("GET",url,true);
            xhr.send();
        }
    }
}

//设置时间
function SetRemainTime() {
    if(curCount==1){
        window.clearInterval(InterValObj);
        document.getElementById("sendButton").style.color="black";
        document.getElementById("sendButton").value="发送验证码";
        document.getElementById("sendButton").disabled = false;
        document.getElementById("sendButton").style.cursor="pointer";

        document.getElementById("registerButton").disabled=true;
        document.getElementById("registerButton").style.cursor="auto";
    }else {
        --curCount;
        document.getElementById("sendButton").style.color="black";
        document.getElementById("sendButton").value=curCount+"s重新发送";
        document.getElementById("registerButton").disabled=false;
        document.getElementById("registerButton").style.cursor="pointer";
    }
}

//选中复选框,按钮颜色改变
function check(clickEle){
    var status = clickEle.checked;
    if(status) {
        document.getElementById("registerButton").style.color="white";
        document.getElementById("registerButton").disabled = false;
        document.getElementById("registerButton").style.cursor="pointer";
        document.getElementById("registerButton").style.background="#2b90d9";
    }

    if(!status){
        document.getElementById("registerButton").style.color="white";
        document.getElementById("registerButton").style.background="gray";
        //提交按钮无法使用
        document.getElementById("registerButton").disabled = true;
        document.getElementById("registerButton").style.cursor="wait";
    }
}

//检查点击注册按钮
function checkRegister () {
    //依次获取注册时输入的值
    var registerPhone = document.getElementById("registerPhone").value;
    var registerPassword = document.getElementById("password").value;
    var verificationCode = document.getElementById("VerificationCode").value;

    if(registerPhone.length==0){
        document.getElementById("error-tip1").innerText="手机号不能为空";
        document.getElementById("registerButton").style.color="black";
        document.getElementById("registerButton").disabled = true;
        //让发送验证码按钮无法点击
        document.getElementById("sendButton").disabled = true;
        document.getElementById("sendButton").style.cursor="auto";
    }

    if(registerPassword.length==0){
        document.getElementById("registerButton").disabled = true;
        document.getElementById("error-tip2").innerText="密码不能为空";
        document.getElementById("registerButton").style.color="black";
    }

    if(verificationCode.length==0){
        document.getElementById("registerButton").disabled = true;
        document.getElementById("error-tip3").innerText = "验证码不能为空";
        document.getElementById("registerButton").style.color="black";
    }


}


// layui.use(['form', 'layedit', 'laydate'], function () {
//     var form = layui.form
//         , layer = layui.layer
//         , layedit = layui.layedit
//         , laydate = layui.laydate;
//
//
//     //自定义验证规则
//     form.verify({
//         pass: [
//             /^[\S]{6,12}$/
//             , '密码必须6到12位，且不能出现空格'
//         ]
//         , content: function (value) {
//             layedit.sync(editIndex);
//         }
//     });
//
//     //监听指定开关
//     form.on('switch(switchTest)', function (data) {
//         layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
//             offset: '6px'
//         });
//         layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
//     });
//
//     //监听提交
//     form.on('submit(demo1)', function (data) {
//         layer.alert(JSON.stringify(data.field), {
//             title: '最终的提交信息'
//         })
//         return false;
//     });
//
//
//     //表单取值
//     layui.$('#LAY-component-form-getval').on('click', function () {
//         var data = form.val('example');
//         alert(JSON.stringify(data));
//     });
//
// });